package com.woniuxy.rabc.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.rabc.dto.DeptDto;
import com.woniuxy.rabc.entity.SysDept;
import com.woniuxy.rabc.service.DeptService;
import com.woniuxy.rabc.util.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("depts")
public class DeptController {
    @Resource
    private DeptService deptService;
    @PostMapping("query")
    public Result queryAll(@RequestBody DeptDto deptDto){
        PageHelper.startPage(deptDto.getCurrentPage(),deptDto.getPageSize(),true);
        List<SysDept> sysDepts = deptService.selAll(deptDto);
        PageInfo<SysDept> pageInfo = new PageInfo<>(sysDepts);
        return Result.ok().data("list",sysDepts).data("total",pageInfo.getTotal());
    }
    @PostMapping
    public Result add(@RequestBody SysDept sysDept){
        int i = deptService.insertSelective(sysDept);
        return Result.ok();
    }
    @GetMapping("{id}")
    public Result queryOne(@PathVariable Long id){
        SysDept sysDept = deptService.selectByPrimaryKey(id);
        return Result.ok().data("dept",sysDept);
    }
    @PutMapping
    public Result update(@RequestBody SysDept sysDept){
        int i = deptService.updateByPrimaryKeySelective(sysDept);
        return Result.ok();
    }
    @DeleteMapping("{id}")
    public Result delete(@PathVariable Long id){
        int i = deptService.deleteByPrimaryKey(id);
        if(i>0){
            return Result.ok();
        }else{
            return Result.error();
        }
    }
}
